package com.example.musicapp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.musicapp.R;
import com.example.musicapp.bean.Music;
import com.example.musicapp.utils.CommonUtils;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.ViewHolder> implements View.OnClickListener {

    private List<Music> mMusicList;
    private OnRecyclerViewItemClickListener mOnRecyclerViewItemClickListener=null;

    public MusicAdapter(List<Music> mMusicList) {
        this.mMusicList = mMusicList;
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.music_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull MusicAdapter.ViewHolder viewHolder, int position) {
        Music music = mMusicList.get(position);
        //转换
        String musicDuration = CommonUtils.formatTime(music.getDuration());
        //设置
        viewHolder.musicPosition.setText((position + 1) + "");
        viewHolder.songName.setText(music.getSong());
        viewHolder.singer.setText(music.getSinger());
        viewHolder.duration.setText(musicDuration);
        //把音乐进
        viewHolder.itemView.setTag(position);
        viewHolder.imageview.setTag(position);
        viewHolder.imageview.setOnClickListener(this);

    }

    @Override
    public int getItemCount() {
        return mMusicList.size();
    }

    @Override
    public void onClick(View v) {
        if(mOnRecyclerViewItemClickListener!=null){
            //getTag方法获取数据
            mOnRecyclerViewItemClickListener.onItemClick(v,(int) v.getTag());

        }
    }

    public void setList(List<Music> mMusicList) {
        this.mMusicList = mMusicList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView musicPosition;
        TextView songName;
        TextView singer;
        TextView duration;
        ImageView imageview;
        public ViewHolder(View view) {
            super(view);
            imageview = view.findViewById(R.id.iv_manage);
            musicPosition = view.findViewById(R.id.item_position);
            songName = view.findViewById(R.id.item_song_name);
            singer = view.findViewById(R.id.item_singer);
            duration = view.findViewById(R.id.item_duration_time);
        }

    }

    /**
     * 对外暴露接口
     */
    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }


    /**
     * 对外暴露方法
     * @param listener 接口
     */
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener){
        this.mOnRecyclerViewItemClickListener=listener;
    }

}
